#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019/2/27 15:08
# @Author  :
# @Site    :
# @File    : hog.py
# @Software: PyCharm
import os

import cv2
import numpy as np

#
from opencv_tool.OpencvHogCreator import createHogVectorFromImg


def getHogFeature(filePath):
    img = cv2.imread(filePath)
    return createHogVectorFromImg(img)


def getHogFeature2(img):
    return createHogVectorFromImg(img)


def makeHogForDir(path, dimen):
    '''计算hog特征'''
    paths = os.listdir(path)
    total_array = np.full(dimen, 0.0)#所有的数据矩阵
    list = []

    for p in paths:
        if not p.startswith("."):
            absPathParent = os.path.join(path, p)
            subFiles = os.listdir(absPathParent)
            # print(absPathParent, "size(" + str(len(subFiles)) + ")")
            for subfile in subFiles:
                if subfile.startswith("."):#忽略一些mac下面的特殊文件
                    continue
                try:
                    absFilePath = os.path.join(absPathParent, subfile)
                    list.append(absFilePath)#因为KNN是监督学习，我们需要记录标签
                    vector = getHogFeature(absFilePath)
                    total_array = np.vstack((total_array, vector))#组装所有的数据矩阵

                except Exception as eror:
                    print(absFilePath + "-*- err sample" + str(eror))
    #total_array 所有的数据矩阵
    total_array = np.delete(total_array, 0, axis=0)

    return total_array.astype(np.float32), list#返回数据和标签
